<?php

namespace App\Service\OrderService\OrderInterfaceService;
use App\Service\BaseService;
use App\Service\Code\Normal;
use App\Model\PssModel\OrderRefund;
use App\Service\OrderService\OrderRefundService\OrderRefundMessage;

/**
 * 云仓售后审核修改
 */
class OrderAfterSaleVerifyModify extends BaseService
{
    /**
     * 订单售后处理
     */
    public function index( $param = [] )
    {
        if( empty($param['requestData']) ) {
            return Normal::returnCode(Normal::FAIL,"requestData数据不能为空");
        }
        $order = $param['requestData'];
        if( empty($order['refundID']) ) {
            return Normal::returnCode(Normal::FAIL,"售后订单refundID不能为空");
        }
        
        if( empty($order['refundType']) ) {
            return Normal::returnCode(Normal::FAIL,"售后方式refundType不能为空");
        }
        
        if( empty($order['sysUserID']) ) {
            return Normal::returnCode(Normal::FAIL,"sysUserID系统管理ID不能为空");
        }
        
        if( empty($order['sysUserName']) ) {
            return Normal::returnCode(Normal::FAIL,"sysUserName参数缺失");
        }
        
        if( !isset($order['refundChannelSettlementMoney']) ) {
            return Normal::returnCode(Normal::FAIL,"refundChannelSettlementMoney参数缺失");
        }
        
        if( !isset($order['refundSupplierSettlementMoney']) ) {
            return Normal::returnCode(Normal::FAIL,"refundSupplierSettlementMoney参数缺失");
        }
        
        if( !isset($order['supplierExpressMoney']) ) {
            return Normal::returnCode(Normal::FAIL,"supplierExpressMoney参数缺失");
        }
        
        if( !isset($order['refundServiceFeeMoney']) ) {
            return Normal::returnCode(Normal::FAIL,"refundServiceFeeMoney参数缺失");
        }
        
        if( !isset($order['replyExpressMoney']) ) {
            return Normal::returnCode(Normal::FAIL,"replyExpressMoney参数缺失");
        }
        
        $refundID = $order['refundID'];
        $orderRefund = OrderRefund::instance()->info(['id'=>$refundID]);
        if( empty($orderRefund) ) {
            return Normal::returnCode(Normal::FAIL,"售后订单数据不存在");
        }
        
        //判断是否已退款审核通过
        if( $orderRefund['refund_amount_status'] == 1 ) {
            return Normal::returnCode(Normal::FAIL,"售后订单已退款审核成功，不能再发起修改操作!");
        }
        
        //判断是否只付了定金
        $isDepositMoneyOnly = OrderPreSellService::instance()->isDepositMoneyOnly($orderRefund['sub_order_id']);
        if( !$isDepositMoneyOnly ) {
            $refund_bd_crm_create = @json_decode($orderRefund['refund_bd_crm_create'],true);
            $order['afterVerifyStatus']             = 1;
            $order['refund_modity']                 = 1;
            $order['refund_extra_value']['vipId']   = $refund_bd_crm_create['requestData']['vipId'];
        } else {
            $order['afterVerifyStatus']             = 1;
            $order['refund_modity']                 = 1;
            $order['refund_extra_value']['vipId']   = 0;
        }
        
        try
        {
            OrderRefund::instance()->startTrans();
            $result = OrderRefundMessage::instance()->orderRefundVerify($orderRefund,$order);
            if( $result['code'] != Normal::SUC ) {
                OrderRefund::instance()->rollback();
                return $result;
            }
            
            OrderRefund::instance()->commit();
            return Normal::returnCode(Normal::SUC,"处理成功");
        } catch ( \Throwable $th ) {
            OrderRefund::instance()->rollback();
            return Normal::returnCode(Normal::FAIL,"修改售后订单出现异常{$th->getMessage()}");
        }
    }
}